<?php
/**
 * Created by PhpStorm.
 * User: wzy12
 * Date: 2018/10/14
 * Time: 11:23
 */

namespace app\admin\controller;


class Ad extends Base
{
    /**
     * 广告位列表
     */
    public function adPosition(){
        $list = model('AdPosition') -> paginate(10);

        $this -> assign('list', $list);
        return $this -> fetch();
    }

    public function adPosition_form(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $result = model('AdPosition') -> operation($data);
            if(!$result['code']){
                return $this -> error($result['msg'], '', ['token' => $this -> request -> token()]);
            }else{
                return $this -> success($result['msg'], url('ad/adPosition'), ['token' => $this -> request -> token()]);
            }
        }else{
            $id = input('id', 0);

            $res = null;
            if($id){
                $res = model('AdPosition') -> find($id);
            }

            $this -> assign('id', $id);
            $this -> assign('res', $res);
            return $this -> fetch();
        }
    }

    public function adPosition_del(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $res = model('AdPosition') -> del($data);
            if($res['code']){
                return $this -> success($res['msg'], url('ad/AdPosition'), ['token' => $this -> request -> token()]);
            }else{
                return $this -> error($res['msg'], '', ['token' => $this -> request -> token()]);
            }
        }else{
            return $this -> error('非法请求', url('index/index'));
        }
    }

    /**
     * 广告列表
     */
    public function ad(){
        $search_key = input();
        unset($search_key['/admin/ad/ad_html']);

        $arr = model('Ad') -> resList($search_key);
        unset($search_key['page']);
        if(!$search_key){
            $search_key = null;
        }

        $this -> assign('search_key', $search_key);
        $this -> assign('ad_position', $arr['ad_position']);
        $this -> assign('list', $arr['list']);
        return $this -> fetch();
    }

    public function ad_form(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $result = model('Ad') -> operation($data);
            if(!$result['code']){
                return $this -> error($result['msg'], '', ['token' => $this -> request -> token()]);
            }else{
                return $this -> success($result['msg'], url('ad/ad'), ['token' => $this -> request -> token()]);
            }
        }else{
            $id = input('id', 0);

            $res = null;
            if($id){
                $res = model('Ad') -> find($id);
            }
            $position = model('AdPosition') -> select();

            $this -> assign('id', $id);
            $this -> assign('res', $res);
            $this -> assign('position', $position);
            return $this -> fetch();
        }
    }

    public function ad_del(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $res = model('Ad') -> del($data);
            if($res['code']){
                return $this -> success($res['msg'], url('ad/ad'), ['token' => $this -> request -> token()]);
            }else{
                return $this -> error($res['msg'], '', ['token' => $this -> request -> token()]);
            }
        }else{
            return $this -> error('非法请求', url('index/index'));
        }
    }

    /**
     * 广告图片上传
     */
    public function ad_upload(){
        $file = request()->file('img');

        $msg = $this -> upload($file);
        if(!$msg['code']){
            return $this -> error($msg['msg']);
        }else{
            return $this -> success($msg['msg'], '', ['img' => $msg['data']['img']]);
        }
    }

    /**
     * 广告排序
     */
    public function adSort(){
        if($this -> request -> isAjax()){
            $data = input('post.');
            $result = model('Ad') -> editSort($data);

            if($result['code']){
                return $this -> success($result['msg'], url('ad/ad'), ['token' => $this -> request -> token()]);
            }else{
                return $this -> error($result['msg'], '', ['token' => $this -> request -> token()]);
            }
        }else{
            return $this -> error('非法请求');
        }
    }

    /**
     * 广告状态
     */
    public function adStatus(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $result = model('Ad') -> editStatus($data);

            if($result['code']){
                return $this -> success($result['msg'], url('ad/ad'));
            }else{
                return $this -> error($result['msg']);
            }
        }else{
            return $this -> error('非法请求');
        }
    }


}